<template>
  <div class="common-search">
    <el-form
      :model="searchForm"
      :inline="true"
      class="search-form"
    >
      <el-form-item
        v-for="field in searchFields"
        :key="field.prop"
        :label="$t(field.labelKey)"
      >
        <el-input
          v-if="field.type === 'input'"
          v-model="searchForm[field.prop]"
          :placeholder="$t(field.placeholder || 'pleaseEnter')"
          clearable
          @keyup.enter="handleSearch"
        />
        
        <el-select
          v-else-if="field.type === 'select'"
          v-model="searchForm[field.prop]"
          :placeholder="$t(field.placeholder || 'pleaseSelect')"
          clearable
        >
          <el-option
            v-for="option in field.options"
            :key="option.value"
            :label="$t(option.labelKey)"
            :value="option.value"
          />
        </el-select>
        
        <el-date-picker
          v-else-if="field.type === 'date'"
          v-model="searchForm[field.prop]"
          type="date"
          :placeholder="$t(field.placeholder || 'pleaseSelectDate')"
          clearable
        />
        
        <el-date-picker
          v-else-if="field.type === 'daterange'"
          v-model="searchForm[field.prop]"
          type="daterange"
          :start-placeholder="$t('startDate')"
          :end-placeholder="$t('endDate')"
          clearable
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="handleSearch">
          {{ $t('search') }}
        </el-button>
        <el-button @click="handleReset">
          {{ $t('reset') }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()

const props = defineProps({
  searchFields: {
    type: Array,
    required: true
  },
  initialValues: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['search', 'reset'])

const searchForm = reactive({ ...props.initialValues })

const handleSearch = () => {
  emit('search', { ...searchForm })
}

const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = props.initialValues[key] || ''
  })
  emit('reset')
}
</script>

<style scoped>
.common-search {
  background: #f5f5f5;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.search-form {
  margin: 0;
}
</style>